function setupGuestbookForm() {
    (function($) {
        $(document).ready(function() {
            $.validator.messages.required = "";
            $.validator.messages.email = "";
            $.validator.messages.maxlength = "";
            $('#commentform').validate({
                rules: {
                    author: {
                        required: true
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    comment: {
                        required: true
                    }
                },

                submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        success: function() {
                            var url = $('#commentform :input:hidden[name=redirect]').attr('value');
                            fetch(url, function(commentsHtml) {
                                $('#comments, #paging').fadeOut(function() {
                                    updateComments(commentsHtml);
                                    pageComments();
                                    $(this).fadeIn('slow');
                                });
                            });
                        },
                        resetForm: true,
                        clearForm: true
                    });
                }
            });
            $('#comments').after('<div id="paging"><a class="prev" href="#"><</a><a class="next" href="#">></a></div>');
            pageComments();
        });
    })(jQuery);
}

function pageComments() {
    (function($) {
        var comments = $('#comments').children();
        for (var c = 0; c < comments.size(); c += 2) {
            comments.slice(c, c + 2).wrapAll('<div class="commentspage"/>');
        }
        $('#comments').cycle({
            fx: 'fade',
            fit: 1,
            containerResize: 0,
            timeout: 0,
            cleartype: 0,
            prev:   '#paging .prev', 
            next:   '#paging .next', 
            after:   function (curr, next, opts) {
                var index = opts.currSlide;
                $('#paging .prev')[index == 0 ? 'hide' : 'show']();
                $('#paging .next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
            }
        });
    })(jQuery);
}

function updateComments(commentsHtml) {
    (function($) {
        $('#comments').empty().append($(commentsHtml).find('#comments').html());
    })(jQuery);
}